"""
重复的代码是万恶之源
    函数式编程思想跟 面向对象的类，封装，继承，多态 差不多。
    都是封装代码，隔离需求，调用父类，实现一对多的方法。
"""

# 不是重点
class SkillData:
    """技能数据"""
    def __init__(self, ids, name, atk_ratio, duration):
        self.ids = ids
        self.name = name
        self.atk_ratio = atk_ratio
        self.duration = duration

    def __str__(self):
        return f"{self.ids}, {self.name}, {self.atk_ratio}, {self.duration}"


# 不是重点
list1 = [
    SkillData(101, "乾坤大挪移", 5, 10),
    SkillData(101, "乾坤大挪移", 8, 5),
    SkillData(101, "乾坤大挪移", 10, 2)
]


# 重点
class SkillConntrol:
    @staticmethod
    def fun_fu(func):
        """函数父类， 提取相同点，调用父类，执行子类"""
        for iss in list1:
            if func(iss):
                yield iss

    @staticmethod
    def fun_yi(info):
        """不同的需求点1"""
        return info.atk_ratio > 6

    @staticmethod
    def fun_er(info):
        """不同的需求点2"""
        return 4 <= info.duration <= 11

    @staticmethod
    def fun_san(info):
        """不同的需求点3"""
        return len(info.name) > 4 and info.duration < 6

    @staticmethod
    def expression():
        """生成器表达式"""
        return (list1[r] for r in range(len(list1)) if list1[r].atk_ratio > 6)


if __name__ == "__main__":
    so = SkillConntrol()
    for i in so.fun_fu(so.fun_san):
        print(i)

    # lambda 表达式
    for j in so.fun_fu(lambda info: len(info.name) > 4 and info.duration < 6):
        print(j)